<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="管理员ID" prop="admin_id">
                <el-input v-model="ruleForm.admin_id"></el-input>
            </el-form-item>
            <el-form-item label="电话" prop="phone">
                <el-input v-model="ruleForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="管理员姓名" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="管理员登录名" prop="loginName">
                <el-input v-model="ruleForm.loginName"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="管理员状态" prop="resource">
                <el-radio-group v-model="ruleForm.resource">
                    <el-radio label="true">启用</el-radio>
                    <el-radio label="false">禁用</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm', ruleForm)">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import axios from "axios";
export default {
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请输入密码"));
            } else {
                if (this.ruleForm.checkPass !== "") {
                    this.$refs.ruleForm.validateField("checkPass");
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请再次输入密码"));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error("两次输入密码不一致!"));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                admin_id: "",
                resource: "",
                pass: "",
                checkPass: "",
                name: "",
                loginName: "",
                phone: ""
            },
            rules: {
                admin_id: [
                    { required: true, message: "请输入管理员ID", trigger: "blur" },
                    { min: 0, max: 5, message: "长度在 0 到 5 个字符", trigger: "blur" }
                ],
                name: [
                    { required: true, message: "请输入管理员姓名", trigger: "blur" },
                    { min: 1, message: "长度至少一个字符", trigger: "blur" }
                ],
                loginName: [
                    { required: true, message: "请输入管理员姓名", trigger: "blur" },
                    { min: 5, message: "长度至少5个字符", trigger: "blur" }
                ],
                resource: [{ required: true, message: "请选择管理员状态", trigger: "change" }],
                pass: [
                    { validator: validatePass, trigger: "blur" },
                    { min: 6, max: 12, message: "长度至少6到12个字符", trigger: "blur" }
                    // {type:"number",message:'必须为数字',trigger: 'blur'}
                ],
                checkPass: [{ validator: validatePass2, trigger: "blur" }],
                phone: [
                    { required: true, message: "请输入电话号码", trigger: "blur" },
                    { min: 7, max: 11, message: "长度7到11位", trigger: "blur" }
                ]
            }
        };
    },
    methods: {
        submitForm(formName, ruleForm) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    alert("submit!");
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
            axios.post("/admin/add", { admin_id: ruleForm.admin_id, admin_name: ruleForm.name, admin_loginName: ruleForm.loginName, admin_pass: ruleForm.pass, admin_status: ruleForm.resource, admin_phone: ruleForm.phone }).then(res => {
                if (res.data) {
                    this.$router.push("/All/adminList");
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
};
</script>

<style >
.el-main {
    text-align: left;
    line-height: 0;
}
/* 输入框设置 */
.el-input__inner {
    width: 360px;
}</style
>>
